package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo2 {

    private static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            executorService.execute(() -> {
                for (int j = 0; j < 100000; j++) {
                    Integer v = threadLocal.get();
                    if(v == null)
                        v = 0;
                    threadLocal.set(v + 1);
                }
                System.out.println(Thread.currentThread() + " -> " + threadLocal.get());
            });
        }
        executorService.shutdown();
    }
}
